package cn.walls1717.wallsmall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.Query;
import cn.walls1717.wallsmall.product.dao.AttrGroupDao;
import cn.walls1717.wallsmall.product.entity.AttrEntity;
import cn.walls1717.wallsmall.product.entity.AttrGroupEntity;
import cn.walls1717.wallsmall.product.service.AttrGroupService;
import cn.walls1717.wallsmall.product.service.AttrService;
import cn.walls1717.wallsmall.product.vo.AttrGroupWithAttrsVO;
import cn.walls1717.wallsmall.product.vo.SpuItemAttrGroupVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Resource
    private AttrService attrService;

    /**
     * 分页列表
     *
     * @param params 分页参数
     * @param catelogId 分类id(catelogId)
     * @return 分页数据
     */
    @Override
    public PageUtils listPages(Map<String, Object> params, Integer catelogId) {
        LambdaQueryWrapper<AttrGroupEntity> lqw = new LambdaQueryWrapper<>();
        // 如果catelogId=0则是查询全部，不需要catelogId查询条件
        if (catelogId != 0) {
            lqw.eq(AttrGroupEntity::getCatelogId, catelogId);
        }

        // 根据查询条件拼装sql
        String key = (String)params.get("key");
        if (!StrUtil.isEmpty(key)) {
            // WHERE (catelog_id = ? AND (attr_group_id = ? OR attr_group_name LIKE ?))
            lqw.and(
                obj -> obj.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getAttrGroupName, key));
        }
        // key为空直接根据catelogId查询 WHERE catelog_id=?
        IPage<AttrGroupEntity> page = page(new Query<AttrGroupEntity>().getPage(params), lqw);
        return new PageUtils(page);
    }

    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catalogId 分类id
     */
    @Override
    public List<AttrGroupWithAttrsVO> listAttrGroupsWithAttrs(Long catalogId) {
        List<AttrGroupEntity> attrGroups = lambdaQuery().eq(AttrGroupEntity::getCatelogId, catalogId).list();
        return attrGroups.stream().map(attrGroup -> {
            List<AttrEntity> attrs = attrService.listAttrsByAttrGroupId(attrGroup.getAttrGroupId());
            AttrGroupWithAttrsVO attrGroupWithAttrsVO = new AttrGroupWithAttrsVO();
            BeanUtil.copyProperties(attrGroup, attrGroupWithAttrsVO);
            attrGroupWithAttrsVO.setAttrs(attrs);
            return attrGroupWithAttrsVO;
        }).collect(Collectors.toList());
    }

    /**
     * 根据spuId与catalogId获取分组与属性相关信息
     *
     * @param spuId spuId
     * @param catalogId catalogId
     * @return SpuItemAttrGroupVO
     */
    @Override
    public List<SpuItemAttrGroupVO> listAttrGroupsWithAttrsBySpuIdCatalogId(Long spuId, Long catalogId) {
        return baseMapper.listAttrGroupsWithAttrsBySpuIdCatalogId(spuId, catalogId);
    }
}